/**
 * Created by Ysssssss on 2021/4/21.
 */

import { put, takeEvery } from 'redux-saga/effects'
import apiType, {
    GET_SHOPPING_MALL_ORDER_LIST,
    CANCEL_ORDER,
    GET_SHOPPING_MALL_ORDER_DETAIL,
    GET_SHOPPING_MALL_AFTER_SALES_ORDER_LIST,
    REFUND_ORDER,
    REFUND_ORDER_AGAIN,
    GET_SHOPPING_MALL_AFTER_SALES_ORDER_DETAIL,
} from "@constants/ActionTypes";
import {getAxios, postAxios, putAxios} from "@utils/api";
import {telephoneValidation} from "@utils/regex";

/**
 * 获取商城订单列表
 * @param startDate
 * @param endDate
 * @param status
 * @param keywordOrderNo
 * @param creator
 * @param warehouseId
 * @param pageNo
 * @param pageSize
 * @param callback
 * @returns {{callback: *, type: string, params: {keywordOrderNo: *, endDate: *, warehouseId: *, pageNo: *, pageSize: *, startDate: *, status: *}}}
 */
export const getShoppingMallOrderList = ({ startDate, endDate, status, keywordOrderNo, creator, warehouseId, pageNo, pageSize }, callback) => {
    let params = { startDate, endDate, status, keywordOrderNo, warehouseId, pageNo, pageSize }
    if (creator && telephoneValidation.test(creator)) {
        params = {
            ...params,
            query_member_type: 1,
            query_member_value: creator
        }
    } else if (creator) {
        params = {
            ...params,
            query_member_type: 2,
            query_member_value: creator
        }
    }
    return {
        type: apiType(GET_SHOPPING_MALL_ORDER_LIST).request,
        params,
        callback
    }
}

/**
 * 取消商城订单
 * @param orderNumber
 * @param reason
 * @param callback
 * @returns {{callback: *, type: string, params: {reason: *, orderNumber: *}}}
 */
export const cancelOrder = ({ orderNumber, reason }, callback) => {
    return {
        type: apiType(CANCEL_ORDER).request,
        params: { orderNumber, reason },
        callback
    }
}

/**
 * 获取商城订单详情
 * @param orderNumber
 * @param callback
 * @returns {{callback: *, type: string, params: *}}
 */
export const getShoppingMallOrderDetail = ({ orderNumber }, callback) => {
    return {
        type: apiType(GET_SHOPPING_MALL_ORDER_DETAIL).request,
        params: {orderNumber},
        callback
    }
}


/**
 * 商城售后订单列表
 * @param startDate
 * @param endDate
 * @param orderNo
 * @param applyNo
 * @param type
 * @param category
 * @param pageNo
 * @param pageSize
 * @param callback
 * @returns {{callback: *, type: string, params: {orderNo: *, endDate: *, pageNo: *, applyNo: *, pageSize: *, type: *, category: *, startDate: *}}}
 */
export const getShoppingMallAfterSalesOrderList = ({ startDate, endDate, orderNo, applyNo, type, category, pageNo, pageSize }, callback) => {
    return {
        type: apiType(GET_SHOPPING_MALL_AFTER_SALES_ORDER_LIST).request,
        params: { startDate, endDate, orderNo, applyNo, type, category, pageNo, pageSize },
        callback
    }
}

/**
 * 获取商品售后单详情
 * @param applyNumber
 * @param callback
 * @returns {{callback: *, type: string, params: {applyNumber: *}}}
 */
export const getShoppingMallAfterSalesOrderDetail = ({ applyNumber }, callback) => {
    return {
        type: apiType(GET_SHOPPING_MALL_AFTER_SALES_ORDER_DETAIL).request,
        params: { applyNumber },
        callback
    }
}

/**
 * 审核退款申请
 * @param applyNumber
 * @param approve
 * @param reason
 * @param callback
 * @returns {{callback: *, type: string, params: {reason: *, approve: *, applyNumber: *}}}
 */
export const refundOrder = ({ applyNumber, approve, reason }, callback) => {
    return {
        type: apiType(REFUND_ORDER).request,
        params: { applyNumber, approve, reason },
        callback
    }
}

/**
 * 退款失败重试
 * @param applyNumber
 * @param marked
 * @param callback
 * @returns {{callback: *, type: string, params: *}}
 */
export const refundOrderAgain = ({ applyNumber, marked }, callback) => {
    return {
        type: apiType(REFUND_ORDER_AGAIN).request,
        params,
        callback
    }
}


const sagas = {
    * [apiType(GET_SHOPPING_MALL_ORDER_LIST).request]({params, callback}) {
        try {
            const response = yield getAxios('/mall/web/orders', params)
            yield put({
                type: apiType(GET_SHOPPING_MALL_ORDER_LIST).success,
                response,
            });
            callback && callback()
        } catch (e) {
            yield put({type: apiType(GET_SHOPPING_MALL_ORDER_LIST).failure});
        }
    },
    * [apiType(CANCEL_ORDER).request]({params, callback}) {
        const { orderNumber, ...otherParams } = params
        try {
            const response = yield postAxios(`/mall/web/order/${orderNumber}`, otherParams)
            yield put({
                type: apiType(CANCEL_ORDER).success,
                response,
            });
            callback && callback()
        } catch (e) {
            yield put({type: apiType(CANCEL_ORDER).failure});
        }
    },
    * [apiType(GET_SHOPPING_MALL_ORDER_DETAIL).request]({params, callback}) {
        const { orderNumber } = params
        try {
            const response = yield getAxios(`/mall/web/order/${orderNumber}`)
            yield put({
                type: apiType(GET_SHOPPING_MALL_ORDER_DETAIL).success,
                response,
            });
            callback && callback()
        } catch (e) {
            yield put({type: apiType(GET_SHOPPING_MALL_ORDER_DETAIL).failure});
        }
    },
    * [apiType(GET_SHOPPING_MALL_AFTER_SALES_ORDER_LIST).request]({params, callback}) {
        try {
            const response = yield getAxios('/mall/web/refunds', params)
            yield put({
                type: apiType(GET_SHOPPING_MALL_AFTER_SALES_ORDER_LIST).success,
                response,
            });
            callback && callback()
        } catch (e) {
            yield put({type: apiType(GET_SHOPPING_MALL_ORDER_DETAIL).failure});
        }
    },
    * [apiType(GET_SHOPPING_MALL_AFTER_SALES_ORDER_DETAIL).request]({params, callback}) {
        const { applyNumber } = params
        try {
            const response = yield getAxios(`/mall/web/refund/${applyNumber}`)
            yield put({
                type: apiType(GET_SHOPPING_MALL_AFTER_SALES_ORDER_DETAIL).success,
                response,
            });
            callback && callback()
        } catch (e) {
            yield put({type: apiType(GET_SHOPPING_MALL_ORDER_DETAIL).failure});
        }
    },
    * [apiType(REFUND_ORDER).request]({params, callback}) {
        const { applyNumber, ...otherParams } = params
        try {
            const response = yield postAxios(`/mall/web/refund/${applyNumber}`, otherParams)
            yield put({
                type: apiType(REFUND_ORDER).success,
                response,
            });
            callback && callback()
        } catch (e) {
            yield put({type: apiType(REFUND_ORDER).failure});
        }
    },
    * [apiType(REFUND_ORDER_AGAIN).request]({params, callback}) {
        const { applyNumber, ...otherParams } = params
        try {
            const response = yield putAxios(`/mall/web/refund/${applyNumber}`, otherParams)
            yield put({
                type: apiType(REFUND_ORDER_AGAIN).success,
                response,
            });
            callback && callback()
        } catch (e) {
            yield put({type: apiType(REFUND_ORDER_AGAIN).failure});
        }
    },
}

export default Object.keys(sagas).map(item => {
    return function * s() {
        yield takeEvery(item, function *(args) {
            try {
                yield sagas[item](args)
            } catch (e) {
                console.log(e)
            }
        })
    }
})